package top.infopub.security.util;


import javax.servlet.http.HttpServletRequest;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import top.infopub.security.vo.AuthorizationVO;


/**
 * Session用户操作工具类
 * @author Awoke
 * @version 2018年9月18日
 * @see SessionUserUtils
 * @since
 */
public final class SessionUserUtils {

    /**
     * SESSION_KEY_USER_DETAIL
     */
    public static final String SESSION_KEY_DETAIL = "SESSION_KEY_USER_DETAIL";

    /**
     * SESSION_KEY_SOURCE
     */
    public static final String SESSION_KEY_SOURCE = "SESSION_KEY_SOURCE";

    /**
     * Session中取得用户详情信息
     * @return
     */
    public static AuthorizationVO getSessionAttributeForUserDtl() {
        return (AuthorizationVO)SecurityUtils.getSubject().getSession().getAttribute(
            SESSION_KEY_DETAIL);
    }

    /**
     * 注入用户详情信息到Session中
     * @return
     */
    public static void setSessionAttributeForUserDtl(AuthorizationVO userObj) {
        SecurityUtils.getSubject().getSession().setAttribute(SESSION_KEY_DETAIL, userObj);
    }

    /**
     * 取得session用户登录名
     * @return
     */
    public static String getSessionUserName() {
        AuthorizationVO v = getSessionAttributeForUserDtl();
        return v == null ? null : v.getUserName();
    }

    /**
     * 取得session对象
     * @return
     */
    public static Session getSession() {
        Subject subject = SecurityUtils.getSubject();
        if (SecurityUtils.getSubject() == null) {
            return null;
        }
        return subject.getSession();
    }

    /**
     * 注入值到Session中
     * @param key
     * @param obj
     * @see
     */
    public static void setSessionAttribute(String key, Object obj) {
        SecurityUtils.getSubject().getSession().setAttribute(key, obj);
    }

    /**
     * 获取会话中的值
     * @return
     */
    public static Object getSessionAttribute(String key) {
        try {
            return SecurityUtils.getSubject().getSession().getAttribute(key);
        }
        catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 删除会话中的值
     * @param key
     */
    public static void removeSessionAttribute(String key) {
        try {
            SecurityUtils.getSubject().getSession().removeAttribute(key);
        }
        catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 取得sessionId
     * @return
     */
    public static String getShiroSessionId() {
        return getSession().getId().toString();
    }

    /**
     * 取得用户ID
     * @return
     */
    public static String getSessionUserId() {
        AuthorizationVO vo = getSessionAttributeForUserDtl();
        if (vo == null) {
            return null;
        }
        return vo.getId();
    }

    /**
     * 判断是否有权限
     * @param role
     * @return
     */
    public static boolean hasRole(String role) {
        Subject currentUser = SecurityUtils.getSubject();
        return currentUser.hasRole(role);
    }

    /**
     * 取得用户的IP地址
     * @return
     */
    public static String getRequestIp() {
        HttpServletRequest request = ((ServletRequestAttributes)RequestContextHolder.getRequestAttributes()).getRequest();
        return IpAddrUtil.getIpAddr(request);
    }

    /**
     * 根据shiro的principal分出用户id和用户名
     * @param principal
     * @return
     */
    public static AuthorizationVO getUserFormPrincipal(String principal) {
        AuthorizationVO vo = new AuthorizationVO();
        String[] name = principal.split("~~");
        vo.setId(name[0]);
        vo.setUserName(name[1]);
        return vo;
    }

}
